<?php
/**
 * test_app
 * @package    spf.applications.test_app
 * @author     Simon Downes <simon@simondownes.co.uk>
 * @copyright  Copyright &copy; 2007, Simon Downes 
 * @license    http://www.opensource.org/licenses/mit-license.php
 */

/**
 * Test application class.
 *
 * @package    spf.applications.test_app
 * @author     Simon Downes <simon@simondownes.co.uk>
 * @copyright  Copyright &copy; 2007, Simon Downes 
 * @license    http://www.opensource.org/licenses/mit-license.php
 */
final class SiTrack extends SPF_Application {
   
   /**
    * Message displayed when there are errors with the user's input.
    */
   const MSG_ERRORS = 'Errors on page.';

   /**
    * Message displayed when a record is updated.
    */
   const MSG_SAVED = 'Record saved successfully.';

   /**
    * Message displayed when a record is deleted.
    */
   const MSG_DELETED = 'Record deleted successfully.';

   /**
    * Message displayed when a record cannot be found.
    */
   const MSG_NOT_FOUND = 'Record not found.';

   /**
    * Message displayed when a file is uploaded.
    */
   const MSG_UPLOADED = 'File uploaded successfully.';

   /**
    * Message displayed when a file upload fails.
    */
   const MSG_UPLOAD_FAILED = 'Error uploading file.';

   /**
    * The name of the application.
    * @var     string
    */
   public $name = 'SiTrack';
   
   /**
    * The version number of the application.
    * @var     string
    */
   public $version = '1.2.0';

   /**
    * Constructor.
    *
    * @return  void
    */
   public function __construct() {
      
      parent::__construct();
      
   } // __construct

   /**
    * Destructor.
    *
    * @return  void
    */
   public function __destruct() {
      
      parent::__destruct();
      
   } // __destruct
   
   /**
    * Application exception handler.
    * All exceptions not caught by the application will be passed to this function.    
    *
    * @param   object    $e   the exception object.
    * @return  void
    */
   public function handler( $e ) {

      $exception_class = get_class($e);
      $exception_name  = substr($exception_class, 4, -10);
      
      switch( $exception_class ) {
         
         case 'SPF_Access_Denied_Exception':
            // if access was denied because we're not signed in then maybe we should make the user sign in
            if( SPF::$session->user === NULL )
               SPF::redirect('users', 'signin');
            break;
         
      }
      
      $template = SPF_Template::factory('smarty');
      
      $template->assign('exception_name', $exception_name);
      $template->assign('exception', $e->to_array());
      
      // if there is a specific view for this exception then use it
      if( $template->exists('exceptions/'. strtolower($exception_name)) )
         $template->display("exceptions/{$exception_name}");
      
      // otherwise use the default exceptions view
      else
         $template->display('exceptions/default');
      
   } // handler
   
   /**
    * Event method called before a template is displayed.
    *
    * @param   string    $template    the name of the template that will be displayed.
    * @return  void
    */
   public function before_display( $template ) {

      SPF::$controller->assign('current_project', SPF::$session->current_project);
      
      switch( SPF::$route['controller'] ) {
         
         case 'tickets':
            SPF::$controller->assign('page_title', SPF::$session->current_project['description']);
            break;
            
         case 'default':
            if( SPF::$route['action'] == 'admin' )
               SPF::$controller->assign('page_title', 'Admin');
            break;
         
         default:
            if( SPF::$session->user != NULL )
               SPF::$controller->assign('page_title', 'Admin');
            break;
         
      } // switch controller name
      
   } // before_display
   
   public function email( $to, $subject, $body, $attachments = array() ) {
      
      if( $attachments && !is_array($attachments) )
         $attachments = array($attachments);
      
      try {
         
         $email = new SPF_Email();
         
         $email->sender  = SPF::$registry->get('mail.from', 'sitrack@localhost');
         $email->add_recipient($to);
         
         $email->subject = $subject;
         $email->body    = $body;
         
         foreach( $attachments as $attachment ) {
            // TODO: add attachments
         }
         
         return $email->send();
         
      }
      catch( Exception $e ) {
         SPF::log($e->getCode(). ' - '. $e->getMessage(), 'error', 'error');
         return false;
      }
      
   } // email
   
   /**
    * Event method called whenever a ticket is assigned to a different user.
    * Sends an email to the new user telling them they have been assigned to the ticket.    
    *
    * @param   array     $ticket    array of ticket information.
    * @return  void
    */
   public function assign_ticket( $ticket ) {
      
      $user = new User();
      $user->fetch( $ticket['assigned_user'] );
      
      $subject = "SiTrack - {$ticket['project_desc']} - Ticket {$ticket['id']}";
      
      $template = SPF_Template::factory('smarty');
      $template->assign('user', $user->to_array());
      $template->assign('ticket', $ticket);
      $template->assign('ticket_link', SPF::get_full_url('tickets', 'view', $ticket['id']));
      
      $body = $template->fetch('tickets/email_assigned');
      
      $this->email($user->email, $subject, $body);
      
   } // assign_ticket
   
   /**
    * Event method called whenever a new user is created.
    * Sends an email to the new user with their username and password and either
    * a link to activate their account or a message saying it's awaiting activation.
    *
    * @param   array     $user       array of user information.
    * @param   string    $password   unencrypted password.
    * @return  void
    */
   public function new_user( $user, $password, $auto_activate ) {
      
      $template = SPF_Template::factory('smarty');
      $template->assign('user_name', $user['user_name']);
      $template->assign('password', $password);
      $template->assign('auto_activate', $auto_activate);
      $template->assign('activate_link', SPF::get_full_url('users', 'activate', array($user['user_name'], $user['password'])));
      $body = $template->fetch('users/email_welcome');
      
      SPF::$app->email($user['email'], 'SiTrack - New User Account', $body);
      
   } // new_user
   
   public function activated_user( $user ) {
      
      $template = SPF_Template::factory('smarty');
      $template->assign('name', $user['display_name']);
      $template->assign('signin_link', SPF::get_full_url('users', 'signin'));
      $body = $template->fetch('users/email_activated');
      
      SPF::$app->email($user['email'], 'SiTrack - Account Activated', $body);
      
   } // activated_user
   
} // test_app

?>
